package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.ForgeItemList;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ForgeItemData;
import labox.innovation.gameserver.network.serverpackets.ForgeItemSucess;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.ItemCompositeConfig;
import labox.innovation.gameserver.templates.item.ItemIdentifyConfig;
import labox.innovation.gameserver.templates.item.ItemInscribeConfig;
import labox.innovation.gameserver.templates.item.ItemStarConfig;
import labox.innovation.gameserver.templates.item.ItemCompositeConfig.ItemCompositeData;
import labox.innovation.gameserver.templates.item.ItemIdentifyConfig.ItemIdentifyData;
import labox.innovation.gameserver.templates.item.ItemStarConfig.ItemStarData;

/**
 *  客户端请求锻造物品 所需要的钱 和 几率
 *   和 物品链
 * @author zjj
 *
 */
public class RequestForgeItem extends L2GameClientPacket {

	private static final String RequestForgeItem = "[C] 0x7e RequestForgeItem";

	protected static final Logger _log = LoggerFactory.getLogger(RequestForgeItem.class.getName());
	
	public static final int OP_IDENTIFY = 0;

	public static final int OP_INSCRIBE = 1;

	public static final int OP_START = 2;

	public static final int OP_COMPOSITIE = 3;

	private long _id; //物品uid

	private int _flag; //打造方式   0:鉴定 1:铭刻2:追星3:合成

	@Override
	public String getType() {
		return RequestForgeItem;
	}

	@Override
	protected void readImpl() {
		_id = readLong();
		_flag = readByte();

	}

	@Override
	protected void runImpl() {

		FuzePcInstance player = getClient().getActiveChar();
		if (player == null) {
			return;
		}

		player.setActiveForgeItemList(null);

		L2ItemInstance item = player.getInventory().getItemByObjectId(_id);
		if (item == null) {
			return;
		}

		if (_flag == OP_IDENTIFY) {
			if (item.isIdentify()) {
				//TODO:已鉴定
				return;
			}

			if (!item.getItem().getCanIdentify()) {
				//TODO:物品 不能被鉴定
				return;
			}

			ItemIdentifyData itemData = ItemIdentifyConfig.getInstance().getItemConfigData(item.getItemLvl());
			getClient().sendPacket(new ForgeItemData(itemData._moneySilverNums, itemData._moneyType, 100, player, item, _flag));
			ForgeItemList forgeList = new ForgeItemList(player, _flag, 0);
			forgeList.setMainItemId(_id);
			player.setActiveForgeItemList(forgeList);

		} else if (_flag == OP_INSCRIBE) {
			if (item.isInscribe()) {
				//TODO:已鉴定
				getClient().sendPacket(new SystemMessage(SystemMessageId.ITEM_CANOT_IDENTIFY));
				return;
			}

			if (!item.getItem().getCanInscribe()) {
				//TODO:物品 不能被鉴定
				getClient().sendPacket(new SystemMessage(SystemMessageId.ITEM_CANOT_INSCRIBE));
				return;
			}

			ItemIdentifyData itemData = ItemInscribeConfig.getInstance().getItemConfigData(item.getItemLvl());
			getClient().sendPacket(new ForgeItemData(itemData._moneySilverNums, itemData._moneyType, 100, player, item, _flag));
			ForgeItemList forgeList = new ForgeItemList(player, _flag, 0);
			forgeList.setMainItemId(_id);
			player.setActiveForgeItemList(forgeList);
		} else if (_flag == OP_START) {

			if (!item.getItem().getCanStar()) {
				//TODO:物品 不能被追星
				getClient().sendPacket(new SystemMessage(SystemMessageId.ITEM_CANOT_STAR));
				return;
			}

//			System.out.println("发送 追星数据");
				
			ItemStarData starData = ItemStarConfig.getInstance().getItemStarData(item.getItemTypeString(), item.getItemLvl(), item.getStarLvl());
			if( starData == null ){
				_log.warn("追星配置数据不存在 :" +item.getItemTypeString() +  " 物品等级: " + item.getItemLvl() + " 追星等级:" + item.getStarLvl(), Config.SERVER_ID);
				return;
			}
			getClient().sendPacket(new ForgeItemData(starData._moneySilverNums, starData._moneyType, starData._upchance, player, item, _flag));
			ForgeItemList forgeList = new ForgeItemList(player, _flag, 2);
			forgeList.setMainItemId(_id);
			player.setActiveForgeItemList(forgeList);
				

		} else if (_flag == OP_COMPOSITIE) {

			if (!item.getItem().getCanComposite()) {
				//TODO:物品 不能被追星
//				System.out.println("物品不能合成");
				getClient().sendPacket(new SystemMessage(SystemMessageId.ITEM_CANOT_COMPOSITE));
				return;
			}

//			System.out.println("发送 合成数据");

			ItemCompositeData compositeData = ItemCompositeConfig.getInstance().getItemCompositeData(item.getItemGrade(), item.getItemLvl());
			if( compositeData == null ){
				getClient().getActiveChar().sendMessage("此装备已达到最高的合成品质, 不能继续合成");
				_log.warn("合成配置数据不存在:" + "物品品质:" + item.getItemGrade() + "物品等级:" + item.getItemLvl() , Config.SERVER_ID);
				return;
			}
			getClient().sendPacket(new ForgeItemData(compositeData._moneySilverNums, compositeData._moneyType, compositeData._chance, player, item, _flag));
			ForgeItemList forgeList = new ForgeItemList(player, _flag, 4);
			forgeList.setMainItemId(_id);
			player.setActiveForgeItemList(forgeList);
		}

	}

}
